package com.mmall.controller.backend;

import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.service.IUserService;
import com.mmall.util.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by hanchao on 2018/1/14.
 */
@RestController
@RequestMapping(value = "/manage/product/")
public class ProductManagerController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IFileService iFileService;

    @RequestMapping("save.do")
    public ServerResponse productSave(HttpSession session, Product product){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录管理员");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //處理产品接口
            return iProductService.saveOrUpdateProduct(product);
        }else{
            return  ServerResponse.createdByErrorMessage("该用户不是管理员，无权限操作");
        }
    }

 @RequestMapping("set_sale_status.do")
    public ServerResponse setSaleStatus(HttpSession session,Integer productId,Integer status){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录管理员");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //處理产品接口
            return iProductService.SetSaleStatus(productId, status);
        }else{
            return  ServerResponse.createdByErrorMessage("该用户不是管理员，无权限操作");
        }
    }
    @RequestMapping("detail.do")
    public ServerResponse detail(HttpSession session,Integer productId){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录管理员");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            return iProductService.manageDetail(productId);
        }else{
            return  ServerResponse.createdByErrorMessage("该用户不是管理员，无权限操作");
        }
    }
    @RequestMapping("list.do")
    public ServerResponse list(HttpSession session,@RequestParam(value = "pageNumber",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录管理员");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            return iProductService.getProductList(pageNum,pageSize);
        }else{
            return  ServerResponse.createdByErrorMessage("该用户不是管理员，无权限操作");
        }
    }
   @RequestMapping("search_product.do")
    public ServerResponse searchProduct(HttpSession session,String productName,Integer productId  ,@RequestParam(value = "pageNumber",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录管理员");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            return iProductService.searchProduct(productName, productId, pageNum,pageSize);
        }else{
            return  ServerResponse.createdByErrorMessage("该用户不是管理员，无权限操作");
        }
    }

    @PostMapping(value = "upload.do")
    public ServerResponse upload(@RequestParam(value = "upload_file",required = false)MultipartFile file, HttpServletRequest request,HttpSession session){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录管理员");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upload(file,path);
            String url = PropertiesUtil.getProperty("ftp.server.ip.http.prefix")+targetFileName;
            Map fileMap = Maps.newHashMap();
            fileMap.put("uri",targetFileName);
            fileMap.put("url",url);

            return ServerResponse.createdBySuccess(fileMap);
        }else{
            return  ServerResponse.createdByErrorMessage("该用户不是管理员，无权限操作");
        }
    }
   @PostMapping(value = "richtext_img_ upload.do")
    public Map richtextImgUpload(@RequestParam(value = "upload_file",required = false)MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        Map map =Maps.newHashMap();
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            map.put("success",false);
            map.put("msg","请登录管理员");
            return map;
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upload(file,path);
            if(StringUtils.isBlank(targetFileName)){
                map.put("success",false);
                map.put("msg","上传失败");
            }
            String url = PropertiesUtil.getProperty("ftp.server.ip.http.prefix")+targetFileName;
            //富文本中对返回值有自己的要求，我们使用simditor,所以按照simditor要求返回
                /* {
                  "success": true/false,
                  "msg": "error message", # optional
                  "file_path": "[real file path]"
                }*/
            map.put("success",true);
            map.put("msg","上传成功");
            map.put("file_path",url);
            response.addHeader("Access-Control-Allow-Headers","X-File-Name");
            return map;
        }else{
            map.put("success",false);
            map.put("msg","该用户不是管理员，无权限操作");
            return map;
        }
    }
}
